package com.example.huwang.utils;

import android.content.Context;
import android.content.SharedPreferences;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

public class SPUtils {

    private static SPUtils instance;
    private Context mCtx;

    /**
     * 保存在手机里面的文件名
     */
    public String mFileName = "hx_data";

    private SPUtils(Context context, String fileName) {

        mCtx = context;
        this.mFileName = fileName;
    }

    public static SPUtils init(Context context, String filename) {
        if (instance == null) {
            synchronized (SPUtils.class) {
                if (instance == null) {
                    instance = new SPUtils(context, filename);
                }
            }
        }
        return instance;
    }

    public static SPUtils getInstance() {
        if (instance == null) {
            throw new IllegalStateException(
                    "you should can getInstance(Context context, String filename) when first time use !");
        }
        return instance;
    }

    /**
     * 保存数据的方法，我们需要拿到保存数据的具体类型，然后根据类型调用不同的保存方法
     *
     * @param key
     * @param object
     */
    public void put(String key, Object object) {

        SharedPreferences sp = mCtx.getSharedPreferences(mFileName,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();

        if (object instanceof String) {
            editor.putString(key, (String) object);
        } else if (object instanceof Integer) {
            editor.putInt(key, (Integer) object);
        } else if (object instanceof Boolean) {
            editor.putBoolean(key, (Boolean) object);
        } else if (object instanceof Float) {
            editor.putFloat(key, (Float) object);
        } else if (object instanceof Long) {
            editor.putLong(key, (Long) object);
        } else {
            editor.putString(key, object.toString());
        }

        SharedPreferencesCompat.apply(editor);
    }

    /**
     * 得到保存数据的方法，我们根据默认值得到保存的数据的具体类型，然后调用相对于的方法获取值
     *
     * @param key
     * @param defaultObject
     * @return
     */
    public Object get(String key, Object defaultObject) {
        SharedPreferences sp = mCtx.getSharedPreferences(mFileName,
                Context.MODE_PRIVATE);

        if (defaultObject instanceof String) {
            return sp.getString(key, (String) defaultObject);
        } else if (defaultObject instanceof Integer) {
            return sp.getInt(key, (Integer) defaultObject);
        } else if (defaultObject instanceof Boolean) {
            return sp.getBoolean(key, (Boolean) defaultObject);
        } else if (defaultObject instanceof Float) {
            return sp.getFloat(key, (Float) defaultObject);
        } else if (defaultObject instanceof Long) {
            return sp.getLong(key, (Long) defaultObject);
        }

        return null;
    }

    /**
     * 移除某个key值已经对应的值
     *
     * @param key
     */
    public void remove(String key) {
        SharedPreferences sp = mCtx.getSharedPreferences(mFileName,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        SharedPreferencesCompat.apply(editor);
    }

    /**
     * 清除所有数据
     */
    public void clear() {
        SharedPreferences sp = mCtx.getSharedPreferences(mFileName,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        SharedPreferencesCompat.apply(editor);
    }

    /**
     * 查询某个key是否已经存在
     *
     * @param key
     * @return
     */
    public boolean contains(String key) {
        SharedPreferences sp = mCtx.getSharedPreferences(mFileName,
                Context.MODE_PRIVATE);
        return sp.contains(key);
    }

    /**
     * 返回所有的键值对
     *
     * @return
     */
    public Map<String, ?> getAll() {
        SharedPreferences sp = mCtx.getSharedPreferences(mFileName,
                Context.MODE_PRIVATE);
        return sp.getAll();
    }

    /**
     * 创建一个解决SharedPreferencesCompat.apply方法的一个兼容类
     *
     * @author zhy
     */
    private static class SharedPreferencesCompat {
        private static final Method sApplyMethod = findApplyMethod();

        /**
         * 反射查找apply的方法
         *
         * @return
         */
        @SuppressWarnings({"unchecked", "rawtypes"})
        private static Method findApplyMethod() {
            try {
                Class clz = SharedPreferences.Editor.class;
                return clz.getMethod("apply");
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }

            return null;
        }

        /**
         * 如果找到则使用apply执行，否则使用commit
         *
         * @param editor
         */
        public static void apply(SharedPreferences.Editor editor) {
            try {
                if (sApplyMethod != null) {
                    sApplyMethod.invoke(editor);
                    return;
                }
            } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
            editor.commit();
        }
    }

    //是否是第一次启动
    private final String IS_FIRST_OPEN_KEY = "is_first_open_key";

    //是否是第一次添加历史数据
    private final String IS_FIRST_ADD_HISTORY = "is_first_add_history";

    //是否同意隐私政策
    private final String IS_AGREE_PRIVACY_KEY = "is_agree_privacy_key";

    //是否使用用户自定义主题
    private final String IS_USE_CUSTOM_THEME_KEY = "is_use_custom_theme_key";

    /**
     * 是否是第一次启动
     */
    public boolean isFirstOpen() {
        return (boolean) get(IS_FIRST_OPEN_KEY, true);
    }

    /**
     * 设置是否是第一次启动
     */
    public void setIsFirstOpen(boolean isFirstOpen) {
        put(IS_FIRST_OPEN_KEY, isFirstOpen);
    }

    /**
     * 是否是第一次启动
     */
    public boolean isFirstAddHistory() {
        return (boolean) get(IS_FIRST_ADD_HISTORY, true);
    }

    /**
     * 设置是否是第一次启动
     */
    public void setIsFirstAddHistory(boolean isFirstAddHistory) {
        put(IS_FIRST_ADD_HISTORY, isFirstAddHistory);
    }

    /**
     * @return 是否同意隐私政策
     */
    public boolean isAgreePrivacy() {
        return (boolean) get(IS_AGREE_PRIVACY_KEY, false);
    }

    public void setIsAgreePrivacy(boolean isAgreePrivacy) {
        put(IS_AGREE_PRIVACY_KEY, isAgreePrivacy);
    }

    public boolean isUseCustomTheme() {
        return (boolean) get(IS_USE_CUSTOM_THEME_KEY, false);
    }

    public void setIsUseCustomTheme(boolean isUseCustomTheme) {
        put(IS_USE_CUSTOM_THEME_KEY, isUseCustomTheme);
    }

}